<?php

/**
 * Class JKSocketArgument
 *
 * @method getPropagation()
 * @method getData()
 * @method JKSocket getClient()
 * @method getEventName()
 * @method callable getCallable()
 *
 * @method $this setPropagation($val)
 * @method $this setData($val)
 * @method $this setClient($val)
 * @method $this setEventName($val)
 * @method $this setCallable($val)
 */
class JKSocketArgument
{
    protected $_data;
    protected $_keyList = [];
    
    public function __construct()
    {
        $this->setPropagation(true);
    }
    
    public function stopPropagation() {
        return $this->setPropagation(false);
    }
    
    public function isPropagation() {
        return $this->getPropagation();
    }
    
    public function data() {
        return $this->_data;
    }
    
    public function __call($function, $args)
    {
        $function = strtolower($function);
        $prefix = substr($function, 0, 3);
    
        if($prefix == 'get') {
            $orikey = substr($function, 3);
            if(isset($this->_keyList[$orikey])) {
                return $this->_data[$this->_keyList[$orikey]];
            } else {
                $key = preg_replace('#([A-Z])#', '_$1', $orikey);
                $key = strtolower(ltrim($key, '_'));
                $this->_keyList[$orikey] = $key;
                return $this->_data[$this->_keyList[$orikey]];
            }
        } else if($prefix == 'set') {
            $orikey = substr($function, 3);
            if(isset($this->_keyList[$orikey])) {
                $this->_data[$this->_keyList[$orikey]] = $args[0];
                return $this;
            } else {
                $key = preg_replace('#([A-Z])#', '_$1', $orikey);
                $key = strtolower(ltrim($key, '_'));
                $this->_keyList[$orikey] = $key;
                $this->_data[$this->_keyList[$orikey]] = $args[0];
                return $this;
            }
        }
        
        throw new Exception('method not exitst: ' . $function . ' in class ' . get_class($this));
    }
}